
package profiler;

import  java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;

import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Iterator;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class ProfilerLog {

	private static Map<String,ProfilerLog> INSTANCES = new HashMap<String, ProfilerLog>();

	private static final Lock createLock_ = new ReentrantLock();

	protected LinkedHashMap<String, ProfileObj> _array = null;
	protected boolean _debug = true;

	public static ProfilerLog getInstance(String name) {
		return getInstance(name, true);
	}

	public static ProfilerLog getInstance(String name, boolean debug) {
		String key = name;
		if (!INSTANCES.containsKey(key)) {
			createLock_.lock();
			try {
				if (!INSTANCES.containsKey(key)) {
					INSTANCES.put(key, new ProfilerLog(debug));
				}
			} finally {
				createLock_.unlock();
			}
		}
		return INSTANCES.get(key);
	}
	
	public ProfilerLog() {
		this(true);
	}

	public ProfilerLog(boolean debug) {
		this._debug = debug;
		this._array = new LinkedHashMap<String, ProfileObj>();
	}
	
	public boolean isDebug() {
		return this._debug;
	}

	public void doStartLog(String id) {
		this.doStartLog(id, false, 0);
	}

	public void doStartLog(String id, boolean log_scribe, int log_scribe_id) {
		if(this._debug == false) return;
		if(id == null ? "" == null : id.equals("")) return;

		long start = System.nanoTime();
		this._array.put(id, new ProfileObj(id, start,log_scribe,log_scribe_id));
	}

	public void doEndLog(String id) {
		if(this._debug == false) return;
		if(id == null ? "" == null : id.equals("")) return;

		if(this._array.containsKey(id)) {
			ProfileObj obj = this._array.get(id);
			long end = System.nanoTime();
			obj.setEndTime(end);
		}
	}

	public void clear() {
		this._array.clear();
	}

	public Map<String, ProfileObj> getLogList() {
		return this._array;
	}

	public String dumpLogConsole() {
		if(this._debug == false) return "";

		String _return = "";
		int count = 1;

		if(this._array != null && this._array.size() > 0) {
			NumberFormat formatter = new DecimalFormat("#0.00000");
			_return = "---------------------------------------------------------------------------\n"
					+ "Module Log Profiler\n"
					+ "No.	ID	Time elapsed in msecs\n";

			Iterator<String> iterator = this._array.keySet().iterator();
			while(iterator.hasNext()) {
				String key = iterator.next();
				ProfileObj obj = this._array.get(key);

				_return += (count++) + "	" + obj.getID();
				if(obj.getIsLogScribe()) _return += "(log to scribe - " + obj.getLogScribeId() + " )";
				_return += "	" + formatter.format(obj.getTime()) + "\n";
			}
		}
		return _return;
	}

	public String dumpLogHtml() {
		if(this._debug == false) return "";

		String _return = "";
		int count = 1;

		if(this._array != null && this._array.size() > 0) {
			NumberFormat formatter = new DecimalFormat("#0.00000");
			_return = "<table style=\"border-collapse: collapse;\" border=1 width=\"100%\" cellpadding=5 cellspacing=5><tbody>"
							+ "<tr><th colspan=\"3\" bgcolor=\"#dddddd\">Module Log Profiler</th></tr>"
							+ "<tr><th width=\"50\">No.</th><th>ID</th><th>Time elapsed in msecs</th></tr>";

			Iterator<String> iterator = this._array.keySet().iterator();
			while(iterator.hasNext()) {
				String key = iterator.next();
				ProfileObj obj = this._array.get(key);			

				_return += "<tr><td align=\"center\">" + (count++) + "</td><td align=\"left\">" + obj.getID();
				if(obj.getIsLogScribe()) _return += "(log to scribe - " + obj.getLogScribeId() + " )";
				_return += "</td><td align=\"left\">" + formatter.format(obj.getTime()) + "</td></tr>";
			}
		}
		return _return;
	}

}

